package com.hxy.controller;

import com.hxy.entity.AuthUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@RequestMapping("auth")
// 日志
@Slf4j
public class AuthUserController {

    @RequestMapping("getAuthUser")
    public Object getAuthUser(){
        // security上下文对象
        // 认证成功之后，保存security的上下文中
        // Authentication:认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        log.info(authentication.toString());
        // 获取主要信息
        AuthUser principal = (AuthUser) authentication.getPrincipal();
        log.info(principal.toString());
        return authentication;
    }

    @RequestMapping("getAuthUser2")
    public Object getAuthUser2(HttpServletRequest request){
        // Principal:认证的主要信息
        Principal principal = request.getUserPrincipal();
        log.info(principal.toString());
        return principal;
    }

    @RequestMapping("getAuthUser3")
    public Object getAuthUser3(Authentication authentication){
        return authentication;
    }

    @RequestMapping("getAuthUser4")
    public Object getAuthUser4(Principal principal){
        return principal;
    }
}
